package com.xjscrm.console.service.common.xxljob.impl;

import com.xjscrm.common.entity.Merchant;
import com.xjscrm.console.dto.circle.CircleTaskMqDTO;
import com.xjscrm.console.mapper.MerchantMapper;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.service.common.xxljob.XxlJobService;
import com.xjscrm.console.service.customer.chat.ChatTalkRecordService;
import com.xjscrm.console.service.customer.group.WwGroupInviteService;
import com.xjscrm.console.service.customer.group.WwGroupService;
import com.xjscrm.console.service.customer.groupSend.SpeedGroupSendService;
import com.xjscrm.console.service.customer.wwCustomer.WwCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class XxlJobServiceImpl implements XxlJobService {

    @Autowired
    private WwGroupInviteService wwGroupInviteService;

    @Autowired
    private ChatTalkRecordService chatTalkRecordService;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private WwCustomerService wwCustomerService;

    @Autowired
    private WwGroupService wwGroupService;

    @Autowired
    private SpeedGroupSendService speedGroupSendService;

    @Override
    public void clearAcceptToday() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                wwGroupInviteService.clearAcceptToday(baseDto);
            }
        }

    }

    @Override
    public void excuteTask() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                wwGroupInviteService.excuteTask(baseDto);
            }
        }
    }

    @Override
    public void syncCustomer() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                wwCustomerService.excuteSyncCustomer(baseDto);
            }
        }
    }

    @Override
    public void syncGroup() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                wwGroupService.excuteSyncGroup(baseDto);
            }
        }
    }


    @Override
    public void syncPersonTag() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                wwCustomerService.excuteSyncPersonTag(baseDto);
            }
        }
    }

    @Override
    public void autoGroupSendTaskSyncByImmediately() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                speedGroupSendService.autoGroupSendTaskSyncByImmediately(baseDto);
            }
        }
    }

    @Override
    public void autoGroupSendTaskSyncByCycle() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                speedGroupSendService.autoGroupSendTaskSyncByCycle(baseDto);
            }
        }
    }

    @Override
    public void autoGroupSendJob() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                BaseDto baseDto = new BaseDto();
                baseDto.setMerId(merchant.getId());
                speedGroupSendService.autoGroupSendJob(baseDto);
            }
        }
    }

    @Override
    public void executeCircleTask() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                CircleTaskMqDTO circleTaskMqDTO = new CircleTaskMqDTO();
                circleTaskMqDTO.setMerId(merchant.getId());
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    log.error("被中断", e);
                }
                //
                rocketMQTemplate.asyncSend("xj_pull_circle_task_topic", MessageBuilder.withPayload(circleTaskMqDTO).build(), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        if (log.isDebugEnabled()) {
                            log.debug("分发拉取朋友圈任务消息成功: merId: {}, msgId: {}", merchant.getId(), sendResult.getMsgId());
                        }
                    }

                    @Override
                    public void onException(Throwable e) {
                        log.error("分发拉取朋友圈任务消息失败", e);
                    }
                });
            }
        }
    }

    @Override
    public void executeCleanChatTalkRecord() {
        List<Merchant> merchantList = merchantMapper.selectByList();
        if (merchantList != null && !merchantList.isEmpty()) {
            for (Merchant merchant : merchantList) {
                chatTalkRecordService.cleanChatTalkRecord(merchant.getId());
            }
        }
    }
}
